<div class="doc-item" id="doc-1227"><h2 class="doc-item-name">read-json<span class="namespace">(clojure.contrib.json.read)</span></h2><ul class="var-arglists"><li>(<span class="var-name">read-json</span>  )</li><li>(<span class="var-name">read-json</span>  s)</li><li>(<span class="var-name">read-json</span> stream eof-error? eof-value)</li></ul><div class="var-docstring"><p>Read one JSON record from s, which may be a String or a
  java.io.PushbackReader.</p></div><div class="var-source"><pre>; clojure/contrib/json/read.clj:143
(defn read-json
  ([] (read-json *in* true nil))
  ([s] (if (string? s)
         (read-json (PushbackReader. (StringReader. s)) true nil)
         (read-json s true nil)))
  ([#^PushbackReader stream eof-error? eof-value]
     (loop [i (.read stream)]
       (let [c (char i)]
         (cond
          ;; Handle end-of-stream
          (= i -1) (if eof-error?
                     (throw (EOFException. &quot;JSON error (end-of-file)&quot;))
                     eof-value)

          ;; Ignore whitespace
          (Character/isWhitespace c) (recur (.read stream))

          ;; Read numbers, true, and false with Clojure reader
          (#{\- \0 \1 \2 \3 \4 \5 \6 \7 \8 \9} c)
          (do (.unread stream i)
              (read stream true nil))

          ;; Read strings
          (= c \&quot;) (read-json-quoted-string stream)

          ;; Read null as nil
          (= c \n) (let [ull [(char (.read stream))
                              (char (.read stream))
                              (char (.read stream))]]
                     (if (= ull [\u \l \l])
                       nil
                       (throw (Exception. (str &quot;JSON error (expected null): &quot; c ull)))))

          ;; Read true
          (= c \t) (let [rue [(char (.read stream))
                              (char (.read stream))
                              (char (.read stream))]]
                     (if (= rue [\r \u \e])
                       true
                       (throw (Exception. (str &quot;JSON error (expected true): &quot; c rue)))))

          ;; Read false
          (= c \f) (let [alse [(char (.read stream))
                               (char (.read stream))
                               (char (.read stream))
                               (char (.read stream))]]
                     (if (= alse [\a \l \s \e])
                       false
                       (throw (Exception. (str &quot;JSON error (expected false): &quot; c alse)))))

          

          ;; Read JSON objects
          (= c \{) (read-json-object stream)

          ;; Read JSON arrays
          (= c \[) (read-json-array stream)

          :else (throw (Exception. (str &quot;JSON error (unexpected character): &quot; c))))))))</pre></div><div class="license"><p>Copyright (c) Rich Hickey. All rights reserved.</p><p>The use and distribution terms for this software are covered by the <a href="http://opensource.org/licenses/eclipse-1.0.php">Eclipse Public License 1.0</a>, which can be found in the file
          epl-v10.html at the root of this distribution. By using this software
          in any fashion, you are agreeing to be bound by the terms of this
          license. You must not remove this notice, or any other, from this
          software.</p></div></div>